﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace OAuth2.Pages
{
    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
        }

        public void OnGet()
        {

        }

        /// <summary>
        /// 1.构建授权Url方法
        /// </summary>
        /// <param name="authSource"></param>
        /// <returns>RedirectUrl</returns>
        public async Task<IActionResult> OnPostAsync()
        {
            var client_id = "43e21bdfe93a542a8af3e44396cbe39ef0c9318c4c0bc640c354a16a7372347e";
            //回调地址进行URL编码。否则请求会出错
            var redirect_uri = System.Web.HttpUtility.UrlEncode("http://localhost:5092/Privacy");
            var url = $"https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code";
            Console.WriteLine(url);
            return Redirect(url);
        }
    }
}
